package com.example.servlet;


import com.alibaba.fastjson.JSON;
import com.example.dao.PostDao;
import com.example.dao.CommentDao;
import com.example.dao.UserDao;
import com.example.dao.imp.PostDaoImp;
import com.example.dao.imp.CommentDaoImp;
import com.example.dao.imp.UserDaoImp;
import com.example.domain.Post;
import com.example.domain.Comment;
import com.example.domain.Users;
import com.example.utils.ResponseUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PostCommentServlet", value = "/PostCommentServlet")
public class PostCommentServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取帖子ID
        String postIdStr = request.getParameter("postId");
        if (postIdStr != null && !postIdStr.isEmpty()) {
            int postId = Integer.parseInt(postIdStr);

            // 调用 PostDao 获取帖子信息
            PostDao postDao = new PostDaoImp();
            Post post = postDao.getPostById(postId);
            UserDao userDao = new UserDaoImp();
            Users usersByUserId = userDao.getUsersByUserId(post.getUserId());
            post.setUser(usersByUserId);

            // 调用 CommentDao 获取帖子下的评论列表
            CommentDao commentDao = new CommentDaoImp();
            List<Comment> commentList = commentDao.getCommentsByPostId(postId);
            for (Comment comment : commentList) {
                Users user = userDao.getUsersByUserId(comment.getUserId());
                comment.setUser(user);
            }
            // 将帖子信息和评论列表存入 request 中
            request.setAttribute("post", post);
            request.setAttribute("commentList", commentList);

            // 转发到 JSP 页面进行渲染
            request.getRequestDispatcher("/postInfo.jsp").forward(request, response);
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "却少postId");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        reader.close();
        Comment comment = JSON.parseObject(jsonBuilder.toString(), Comment.class);

        CommentDao commentDao = new CommentDaoImp();
        commentDao.addComment(comment);

        // 调用 PostDao 获取帖子信息
        PostDao postDao = new PostDaoImp();
        Post post = postDao.getPostById(comment.getPostId());
        UserDao userDao = new UserDaoImp();
        Users usersByUserId = userDao.getUsersByUserId(post.getUserId());
        post.setUser(usersByUserId);

        // 调用 CommentDao 获取帖子下的评论列表
        List<Comment> commentList = commentDao.getCommentsByPostId(comment.getPostId());
        for (Comment comment1 : commentList) {
            Users user = userDao.getUsersByUserId(comment1.getUserId());
            comment1.setUser(user);
        }
        // 将帖子信息和评论列表存入 request 中
        request.setAttribute("post", post);
        request.setAttribute("commentList", commentList);
        // 转发到 JSP 页面进行渲染
        request.getRequestDispatcher("/postInfo.jsp").forward(request, response);
    }

}
